package com.biubiux2;

import com.biubiux2.codec.MessageEncryptAndDecryptHandler;
import com.biubiux2.codec.MessageProtocolCodec;
import com.biubiux2.codec.compress.MessageCompressHandler;
import com.biubiux2.handler.HeartBeatServerHandler;
import com.biubiux2.handler.LoginHandler;
import com.biubiux2.handler.MessageHandlerAdapter;
import com.biubiux2.handler.ServerEncryptHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author Lijinbao
 * @since 8/5/2022 5:12 PM
 */
@Slf4j
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new LengthFieldBasedFrameDecoder(10240, 0, 4, 0, 0));
        pipeline.addLast(new MessageProtocolCodec());
        pipeline.addLast(new IdleStateHandler(60, 50, 60, TimeUnit.SECONDS));
        pipeline.addLast(new HeartBeatServerHandler());
        pipeline.addLast(new MessageCompressHandler());
        pipeline.addLast(new ServerEncryptHandler());
        pipeline.addLast(new LoginHandler());
        pipeline.addLast(new MessageHandlerAdapter());
    }

}
